// pages/index/plan/plan.js

const { getxhljMeetingScheduleList } = require("../../../utils/http/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    meetingScheduleList: [],
    filteredMeetingList: [],
    searchValue: '',
    meetList: [
      {
        pic: "https://img2023.gcsis.cn/2024/4/828078254f904f3f834141b704cae9ed.jpg",
        title: "企业消防安全基础培训",
        date: '4月19日',
        time: "09:00-11:45",
        address: "安恒大厦2层"
      }, {
        pic: "https://img2023.gcsis.cn/2023/5/a840f604ec3b4a8a81b2e810e62f6314.jpg",
        title: "企业消防安全演练与应急响应",
        date: '4月25日',
        time: "08:00-12:00",
        address: "浙江省嘉兴市乌镇镇虹桥路165号安恒书院"
      }, {
        pic: "https://img2023.gcsis.cn/2024/4/5cfc2ee523b04200b9bb616d585db2dc.jpg",
        title: "生态合作伙伴大会",
        date: '5月8日',
        time: "09:00-11:50",
        address: " 杭州白金汉爵大酒店 368星空厅"
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    _this.getxhljMeetingScheduleList()
  },

  getxhljMeetingScheduleList() {
    let _this = this;

    getxhljMeetingScheduleList().then((res) => {
      console.log("日程列表", res);
      if (res && res.rows) {  // 确保res和res.data都存在  
        const meetingScheduleList = res.rows;
        meetingScheduleList.forEach((item) => {
          // 将字符串转化为Date对象
          let date = new Date(item.meetingSchedule);
          // 获取月份和日期
          let month = date.getMonth() + 1; // 月份从0开始，所以要加1
          let day = date.getDate();
          // 获取小时数
          let hours = date.getHours();
          let period = hours < 12 ? '上午' : '下午';
          // 格式化为中文的 "7月22日 上午" 或 "7月22日 下午"
          let formattedDate = `${month}月${day}日 ${period}`;
          item.meetingSchedule = formattedDate;
          item.xhljActivityVoList.forEach((item)=>{
              let image = item.activityImage.split(",")
              item.activityImageFirst = image[0]
          })
        })
        _this.setData({
          meetingScheduleList: meetingScheduleList,
          filteredMeetingList: meetingScheduleList,
        });
      }
    }).catch((error) => {
      console.error('请求活动详情失败:', error);
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 搜索输入处理
   */
  onSearchInput(e) {
    const searchValue = e.detail;
    this.setData({
      searchValue: searchValue
    });
    this.filterMeetings(searchValue);
  },

  /**
   * 搜索确认处理
   */
  onSearchConfirm(e) {
    const searchValue = e.detail;
    this.setData({
      searchValue: searchValue
    });
    this.filterMeetings(searchValue);
  },

  /**
   * 过滤会议数据
   */
  filterMeetings(searchValue) {
    const { meetingScheduleList } = this.data;
    if (!searchValue.trim()) {
      this.setData({
        filteredMeetingList: meetingScheduleList
      });
      return;
    }

    const filteredList = meetingScheduleList.map(meeting => {
      const filteredActivities = meeting.xhljActivityVoList.filter(activity => {
        const titleMatch = activity.activityTitle.toLowerCase().includes(searchValue.toLowerCase());
        const addressMatch = activity.activityLocation.toLowerCase().includes(searchValue.toLowerCase());
        return titleMatch || addressMatch;
      });

      if (filteredActivities.length > 0) {
        return {
          ...meeting,
          xhljActivityVoList: filteredActivities
        };
      }
      return null;
    }).filter(meeting => meeting !== null);

    this.setData({
      filteredMeetingList: filteredList
    });
  },

  /**
   * 清空搜索
   */
  onSearchClear() {
    this.setData({
      searchValue: '',
      filteredMeetingList: this.data.meetingScheduleList
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})